<template>
  <!-- 定义登录页面的模板结构 -->
  <div class="login-container">
    <!-- 右上角图片 -->
    <img class="page-logo" src="../../images//logo.png" alt="Logo">

    <div class="login-content-wrap">
      <!-- 使用Element UI的表单组件，包含用户名和密码输入框，以及登录按钮 -->
      <el-form ref="loginForm" :model="loginForm" class="login-form" autocomplete="on" label-position="left">

        <!-- 登录标题区域 -->
        <div class="title-container">
          <h3 class="title">教务管理系统</h3>
        </div>

        <!-- 用户名输入框 -->
        <el-form-item prop="number">
          <!-- 用户图标容器 -->
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <!-- 输入框组件，双向绑定到loginForm.number，有校验规则 -->
          <el-input
            ref="number"
            v-model="loginForm.number"
            placeholder="number"
            name="number"
            type="text"
            tabindex="1"
            autocomplete="on"
          />
        </el-form-item>

        <!-- 密码输入框，带有Caps Lock提示 -->
        <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
          <el-form-item prop="password">
            <!-- 密码图标容器 -->
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <!-- 密码输入框组件，动态类型（明文/密文），双向绑定到loginForm.password，有校验规则 -->
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="Password"
              name="password"
              tabindex="2"
              autocomplete="on"
              @keyup.native="checkCapslock"
              @blur="capsTooltip = false"
              @keyup.enter.native="handleLogin"
              v-bind:users="res"
            />
            <!-- 显示/隐藏密码按钮 -->
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>
        </el-tooltip>

        <!-- 登录按钮，加载状态与点击事件 -->
        <el-button
          :loading="loading"
          type="primary"
          style="width:100%;margin-bottom:30px;"
          @click.native.prevent="handleLogin"
          v-bind:users="users"
        >Login</el-button>
      </el-form>
    </div>

  </div>
</template>

<script>

export default {
  // 组件名称
  name: 'Login',
  data() {
    return {
      userId : '',
      // 表单数据模型
      loginForm: { // 表单提交的数据
        number: '',
        password: ''
      },
      // 密码显示类型（明文/密文）
      passwordType: 'password',
      // Caps Lock提示开关
      capsTooltip: false,
      // 登录按钮加载状态
      loading: false,
      // 第三方登录弹窗可见性
      showDialog: false,
      // 重定向路由
      redirect: undefined,
      // 其他查询参数
      otherQuery: {}
    }
  },
  // 监听路由变化，处理重定向和查询参数
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created() {
  },
  mounted() {
    if (this.loginForm.number === '') {
      this.$refs.number.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  destroyed() {
  },
  methods: {
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(res => {
            if (res.code == 1) {
              this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
              this.$message.success('登录成功')
              // if(res.data.required){
              //   this.$router.push('/schoolmanage/index2')
              // }
            } else {
              this.$message.error(res.msg)
            }

            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

.page-logo {
  position: absolute;
  /* 使用绝对定位 */
  top: 20px;
  /* 调整为所需的顶部距离 */
  left: 20px;
  /* 调整为所需的右侧距离 */
  width: ¾em ;
  /* 设置图片宽度，可根据实际图片尺寸和设计需求调整 */
  height: auto;
  /* 保持原始比例 */
  z-index: 1;
  /* 可能需要设置一个合适的 z-index，确保图片位于其它元素之上 */

  /* 可选，添加过渡动画等样式 */
  transition: opacity 0.3s ease-in-out;
}

/* 如果需要，可以为不同屏幕尺寸设置响应式样式 */
@media (max-width: 768px) {
  .page-logo {
    width: 50px;
    /* 示例：在较小屏幕尺寸下缩小图片宽度 */
  }
}

.login-content-wrap {
  background-color: rgba(255, 255, 255, 0.9);
  /* 白色背景，透明度50% */
  border-radius: 10px;
  /* 可选，添加圆角以增强视觉效果 */
  padding: 20px;
  /* 可选，添加内边距以增加内容与边界的距离 */
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  /* 可选，添加阴影以增加立体感 */
  width: 450px;
  height: 330px;
  margin: 190px auto 0;
  display: flex;
  align-items: center;
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: black;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }

  /* 添加背景图相关属性 */
  background-image: url('../../images/school1.jpg');
  background-repeat: no-repeat;
  /* 可根据需求更改重复模式 */
  background-position: center;
  /* 可根据需求调整背景图片位置 */
  background-size: cover;
  /* 或者 contain，根据需要调整背景图片大小 */
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: -130px auto 40px auto;
      text-align: center;
      font-weight: bold;
      color: black;
      letter-spacing: 4px;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
